package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.cardmanagement.util.AppCardFaceAssoc;
import com.turing.post.cardmanagement.util.AppCardFaceAssocDao;
import com.turing.post.cardmanagement.util.CardATS;
import com.turing.post.cardmanagement.util.CardATSBusiness;
import com.turing.post.cardmanagement.util.CardATSDao;
import com.turing.post.cardmanagement.util.CardFace;
import com.turing.post.cardmanagement.util.CardFaceDao;
import com.turing.post.cardmanagement.util.CardStock;
import com.turing.post.cardmanagement.util.CardStockDao;
import com.turing.post.cardmanagement.util.MakeCardControl;
import com.turing.post.cardmanagement.util.MakeCardControlDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "cardmanagement.impl.CardATSBusinessImpl")
@Remote
@Local
public class CardATSBusinessImpl implements CardATSBusiness {

	@EJB(mappedName = "cardmanagement.impl.CardATSDaoImpl")
	private CardATSDao catsDao;

	@EJB(mappedName = "cardmanagement.impl.CardStockDaoImpl")
	private CardStockDao csDao;

	@EJB(mappedName = "cardmanagement.impl.MakeCardControlDaoImpl")
	private MakeCardControlDao makeCardControlDaoImpl;

	@EJB(mappedName = "cardmanagement.impl.CardFaceDaoImpl")
	private CardFaceDao cardFaceDaoImpl;

	@EJB(mappedName = "cardmanagement.impl.AppCardFaceAssocDaoImpl")
	private AppCardFaceAssocDao appCardFaceAssocDao;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	/**
	 * 异常抛出方法
	 * 
	 * @param error
	 * @throws PCPSApplicationException
	 */
	@SuppressWarnings("unused")
	private void errException(String error) throws PCPSApplicationException {
		throw new PCPSApplicationException(
				PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
				error);
	}

	/**
	 * 卡发行预处理
	 */
	@Override
	public String pretreatCardATS(PCPSWebInput webInput, CardATS cats,
			BigInteger appId) throws PCPSApplicationException {
		// 1.参数合法性检查
		// 入口参数检查
		if (webInput == null || webInput.getOperId() == null) {
			errException("登录信息为空!");
		}
		if (cats == null) {
			errException("卡ATS信息为空!");
		}
		if (PCPSUtil.isEmpty(cats.getATS())) {
			errException("CPU卡ATS号为空!");
		}
		if (PCPSUtil.isEmpty(cats.getCardSeqNo())) {
			errException("卡序号为空!");
		}
		CardStock cs = csDao.getCardStockByCardSeqNoOrCardNo(cats
				.getCardSeqNo());
		if (cs == null) {
			errException("卡库存不存在卡号或序号为[" + cats.getCardSeqNo() + "]的卡!");
		}
		// 检索制卡控制批次
		MakeCardControl mcc = makeCardControlDaoImpl
				.getMakeCardControlByBatchNo(cs.getBatchNo());
		if (mcc == null) {
			errException("卡序号[" + cats.getCardSeqNo() + "]的制卡信息表未找到!");
		}
		// 检查应用卡面关联表是否存在此应用
		CardFace cf = cardFaceDaoImpl.getCardFaceByCardFaceId(mcc
				.getCardFaceId());
		AppCardFaceAssoc acfa = new AppCardFaceAssoc();
		acfa.setAppId(appId);
		acfa.setCardFaceId(cf.getCardFaceId());
		if (appCardFaceAssocDao.getAppCardFaceAssoc(acfa) == null) {
			errException("此卡对应卡面关联的应用不存在!");
		}
		// 检查是否已经存在ats信息
		List<CardATS> atslist = catsDao.getCardATSByAts(cats.getATS());
		if (atslist != null && atslist.size() > 0) {
			errException("已存在ATS号为:" + cats.getATS() + "的卡应用关联信息!");
		}
		atslist = catsDao.getCardATSByCardSeqNo(cats.getCardSeqNo());
		if (atslist != null && atslist.size() > 0) {
			errException("已存在卡序号为:" + cats.getCardSeqNo() + "的卡应用关联信息!");
		}
		atslist = catsDao.getCardATSByCardNo(cats.getCardNo());
		if (atslist != null && atslist.size() > 0) {
			errException("已存在卡号为:" + cats.getCardNo() + "的卡应用关联信息!");
		}
		// 2.创建根目录的APDU

		// 3.创建应用目录APDU

		// 4.新增卡ATS信息表
		cats.setCardNo(cs.getCardNo());
		catsDao.insert(cats);

		// 日志记录
		sysOperLogDaoImpl
				.insertSysOperLogInfo(webInput, SysOperLog.operType_Add,
						"卡发行预处理", "CardNo:" + cats.getCardNo());

		// 5.返回APDU
		return "";
	}

}
